import { Controller, Get, Post, UseGuards, Body, UseInterceptors, UploadedFiles, Param, Query } from '@nestjs/common';
import { ProductService } from './product.service';
import { Product } from './product.entity';
import { FileFieldsInterceptor } from '@nestjs/platform-express';
import { AuthGuard } from '@nestjs/passport';
import { UploadDto } from './upload.dto';

@Controller('product')
export class ProductController {
    constructor(private productService: ProductService) {}

    @Get("findAll")
    findAll(): Promise<Product[]> {
        return this.productService.findAll();
    }

    @Get("findOne")
    async findOne(@Query() params) {
        const product: Promise<Product>  = this.productService.findOne(params.id);
        return await product;
    }

    @Post("addOne")
    @UseGuards(AuthGuard('jwt'))
    @UseInterceptors(FileFieldsInterceptor([
        { name: 'productImg', maxCount: 1 },
        { name: 'rotationBig', maxCount: 99 },
        { name: 'rotationSmall', maxCount: 99 },
    ]))
    async addOne(@Body() body: UploadDto, @UploadedFiles() files: { productImg?: Express.Multer.File[], rotationBig?: Express.Multer.File[], rotationSmall?: Express.Multer.File[] }): Promise<Product> {
        const product: Promise<Product>  = this.productService.addOne(body.name, body.price, body.mainType, body.secondType, body.introduce, body.info, files);
        return product;
    }

}
